<?php
$_categories     = $this->getCategories();
$_collectionSize = count($_categories);

if (!$_collectionSize) :
    return;
endif;

$columnsCount = $this->getColumnCount();
$showImage    = $this->getShowImage();
$height       = $this->getImageHeight();
$width        = $this->getImageWidth();
$maxCategoryCount    = $this->getCategoryCount();
$maxSubcategoryCount = $this->getSubcategoryCount();
$i = 0;
?>

<div class="easycatalogimg">
<?php foreach ($_categories as $_category) : ?>

    <?php if ($i >= $maxCategoryCount): break; endif; ?>

    <?php if($i++%$columnsCount==0): ?>
    <ul class="products-grid category-grid easycatalogimg-cols-<?php echo $columnsCount ?>">
    <?php endif; ?>

    <li class="item">
        <h5 class="category-name parent-category"><a href="<?php echo $_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h5>
        <?php if ($showImage) : ?>
            <a href="<?php echo $_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="product-image">
                <?php if(!$resizeImage = $this->getResizeImage()): ?>
                    <?php
                        $style = '';
                        if (!empty($width)):
                            $style = 'width: ' . (is_numeric($width) ? $width . 'px' : $width);
                        elseif (!empty($height)):
                            $style = 'height: ' . (is_numeric($height) ? $height . 'px' : $height);
                        endif;
                    ?>
                    <img src="<?php echo $this->getImage($_category) ?>" style="<?php echo $style ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                <?php else: ?>
                    <img src="<?php echo Mage::helper('easycatalogimg/image')->resize($this->getImage($_category), $width, $height) ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
                <?php endif; ?>
            </a>
        <?php endif; ?>
        <?php
        if ($maxSubcategoryCount) :
            $j = 0;
            $_subcategories = $_category->getSubcategories();
            $_count         = count($_subcategories);
            // display More link, if more than 1 subcategories are not shown, otherwise - display last category
            $_displayMoreLink = $_count > $maxSubcategoryCount + 1;
            if ($_count) : ?>
                <ul class="list-subcategories">
                <?php foreach ($_subcategories as $_subcategory) :
                    if ($_displayMoreLink && ($j > $maxSubcategoryCount - 1)): ?>
                        <li>
                            <a href="<?php echo $_category->getUrl() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="link-more"><?php echo $this->__('More in %s...', $this->htmlEscape($_category->getName())); ?></a>
                        </li>
                        <?php break;
                    endif; ?>
                    <li>
                        <a href="<?php echo $_subcategory->getUrl() ?>"
                            title="<?php echo $this->htmlEscape($_subcategory->getName())?>"
                            class="category-name child-category"><?php
                            echo $this->htmlEscape($_subcategory->getName());
                        ?></a>
                    </li>
                    <?php $j++; ?>
                <?php endforeach;?>
                </ul>
            <?php
            endif;
        endif; ?>
    </li>

    <?php if($i%$columnsCount==0 || $i==$_collectionSize): ?>
    </ul>
    <?php endif; ?>

<?php endforeach; ?>
</div>

<script type="text/javascript">decorateGeneric($$('ul.category-grid'), ['odd','even','first','last'])</script>
